/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package glamourbeautyclinic.customer;


import java.awt.Dimension;
import java.awt.Toolkit;
import images.GlamourImage;
import java.awt.Color;
import java.awt.Component;
import java.awt.Point;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.RowSorter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import util.DBConnection;




/**
 *
 * @author anh
 */
public class FrmAppointment extends javax.swing.JInternalFrame {
    
    GlamourImage GI = new GlamourImage();
    DBConnection dc;
    DefaultComboBoxModel custModel, slotModel, catModel, serModel, statusModel;
    DefaultTableModel staffModel, serviceModel;
    ListSelectionModel staffSel, serviceSel;
    ArrayList param = new ArrayList();
    Vector transpoter,listCust = new Vector();
    String User;
    int remainFac, availStaff;
    boolean viewOnly;
    /**
     * Creates new form frmSearchCustomer
     */
    public FrmAppointment(DBConnection dc) {
        //this.setContentPane(new ImageBackgroundPanel(new ImageIcon(GI.getClass().getResource("Frame.png"))));
        initComponents();
        
        GI.addIconFor(this);
        this.dc = dc;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        // Can be resize height parameter to proper scale
        this.setLocation((screenSize.width - this.getWidth()) / 2, 10);
        
        serviceModel = (DefaultTableModel)tblAppDetails.getModel();
        RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(serviceModel);
        tblAppDetails.setRowSorter(sorter);
        tblAppDetails.setComponentPopupMenu(pmnAppDetails);
        
        staffModel = (DefaultTableModel)tblStaffAvail.getModel();
        sorter = new TableRowSorter<TableModel>(staffModel);
        tblStaffAvail.setRowSorter(sorter);
        staffSel = tblStaffAvail.getSelectionModel();
        staffSel.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent evt) {
                    tblStaffValueChanged(evt);
            }
        });
        
        cboCust.setRenderer(new cboCustRender());
        cboSlot.setRenderer(new cboRender());
        cboCat.setRenderer(new cboRender());
        cboSer.setRenderer(new cboSerRender());
        cboStatus.setRenderer(new cboRender());
        
        initComboBox();
    }

    public FrmAppointment(DBConnection dc, String CustName, boolean viewOnly) {
        this(dc);
        for (int i = 0; i < cboCust.getItemCount(); i++) {
            Customer cust = (Customer)cboCust.getItemAt(i);
            if (cust.custName.equalsIgnoreCase(CustName)) {
                cboCust.setSelectedIndex(i);
            }
        }
        if (viewOnly) {
            this.setTitle("Appointment management (view only)");
            this.viewOnly = viewOnly;
            tblStaffAvail.setSelectionModel(new NullSelectionModel());
            tblAppDetails.setSelectionModel(new NullSelectionModel());
            staffModel.setRowCount(0);
            btnAddSer.setEnabled(false);
            btnPreBill.setEnabled(false);
            btnFind.setEnabled(false);
            btnSelectAll.setEnabled(false);
            mniSelectAll.setEnabled(false);
            tblAppDetails.setEnabled(false);
        }
    }
    
    private void initComboBox() {
        initCustComboBox();
        initSlotComboBox();
        initCatComboBox();
        initStatusComboBox();
    }
    
    private void getAuthority() {
        if (User == null) {
            try {
                User = (String)getDesktopPane().getTopLevelAncestor().getClass().getDeclaredField("user").get(getDesktopPane().getTopLevelAncestor());
            } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
                Logger.getLogger(FrmCustomer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    private void initCustComboBox(){
        custModel = new DefaultComboBoxModel();
        custModel.addElement(new Customer("",""));
        String sql = "SELECT CustID, CustName FROM Customer ORDER BY CustName";
        dc.rs = dc.selectCommandNoPara(sql);
        try {
            while (dc.rs.next()) {
                custModel.addElement(new Customer(dc.rs.getString("CustID"),
                                          dc.rs.getString("CustName")));    
            }
        } catch (SQLException ex) {
            Logger.getLogger(FrmAppointment.class.getName()).log(Level.SEVERE, null, ex);
        }
        cboCust.setModel(custModel);
    }
    
    private void initSlotComboBox(){ 
        slotModel = new DefaultComboBoxModel();
        slotModel.addElement("1: 08 AM - 09 AM");
        slotModel.addElement("2: 09 AM - 10 AM");
        slotModel.addElement("3: 10 AM - 11 AM");
        slotModel.addElement("4: 11 AM - 12 AM");
        slotModel.addElement("5: 01 PM - 02 PM");
        slotModel.addElement("6: 03 PM - 04 PM");
        slotModel.addElement("7: 05 PM - 06 PM");
        slotModel.addElement("8: 07 PM - 08 PM");
        cboSlot.setModel(slotModel);
    }
    
    private void initCatComboBox(){
        catModel = new DefaultComboBoxModel();
        String sql = "SELECT * FROM Category ORDER BY CatName";
        dc.rs = dc.selectCommandNoPara(sql);
        try {
            while (dc.rs.next()) {
                if (dc.rs.getBoolean("IsLock") == false)
                    catModel.addElement( dc.rs.getString("CatName"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(FrmAppointment.class.getName()).log(Level.SEVERE, null, ex);
        }
        cboCat.setModel(catModel);
        cboCatItemStateChanged(null);
    }
    
    private void initSerComboBox(){
        serModel = new DefaultComboBoxModel();
        String sql = "SELECT * FROM vw_cate_services WHERE CatName = ? AND IsLock = 'False' ORDER BY ServiceName";
        param.clear();
        param.add(cboCat.getSelectedItem());
        dc.rs = dc.prepareSelect(dc.preSt_getSer, sql, param);
        
        try {
            while (dc.rs.next()) {
                serModel.addElement( new Service(dc.rs.getString("ServiceID"),
                                                 dc.rs.getString("ServiceName")));
            }
        } catch (SQLException ex) {
            Logger.getLogger(FrmAppointment.class.getName()).log(Level.SEVERE, null, ex);
        }
        cboSer.setModel(serModel);
        
        if (serModel.getSize() != 0)
            cboSerItemStateChanged(null);
        else 
            btnSubmit.setEnabled(false);
    }
    
    private void initStatusComboBox(){
        statusModel = new DefaultComboBoxModel();
        statusModel.addElement("Pending");
        statusModel.addElement("Canceled");
        statusModel.addElement("Finished");
        cboStatus.setModel(statusModel);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pmnAppDetails = new javax.swing.JPopupMenu();
        mniSelectAll = new javax.swing.JMenuItem();
        mniDeselect = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mniFinish = new javax.swing.JMenuItem();
        mniCancel = new javax.swing.JMenuItem();
        mniDelete = new javax.swing.JMenuItem();
        pnlAppMaster = new javax.swing.JPanel();
        lblCustName = new javax.swing.JLabel();
        chkHasApp = new javax.swing.JCheckBox();
        pnlButton = new javax.swing.JPanel();
        btnMakeApp = new javax.swing.JButton();
        btnAddSer = new javax.swing.JButton();
        btnPreBill = new javax.swing.JButton();
        btnFind = new javax.swing.JButton();
        cboCust = new javax.swing.JComboBox();
        pnlAppDetails = new javax.swing.JPanel();
        pnlDetailsChild = new javax.swing.JPanel();
        btnSelectAll = new javax.swing.JButton();
        btnDeselect = new javax.swing.JButton();
        btnFinish = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jspAddDetails = new javax.swing.JScrollPane();
        tblAppDetails = new javax.swing.JTable();
        lblSerMsg = new javax.swing.JLabel();
        pnlService = new javax.swing.JPanel();
        lblSlot = new javax.swing.JLabel();
        lblCat = new javax.swing.JLabel();
        lblSer = new javax.swing.JLabel();
        lblStatus = new javax.swing.JLabel();
        cboSlot = new javax.swing.JComboBox();
        cboCat = new javax.swing.JComboBox();
        cboSer = new javax.swing.JComboBox();
        cboStatus = new javax.swing.JComboBox();
        lblStaff = new javax.swing.JLabel();
        txtStaff = new javax.swing.JTextField();
        pnlStaffAvail = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblStaffAvail = new javax.swing.JTable();
        lblRes = new javax.swing.JLabel();
        btnSubmit = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        lblAvailFac = new javax.swing.JLabel();
        lblAvailStaff = new javax.swing.JLabel();
        txtAppID = new javax.swing.JTextField();
        txtStatus = new javax.swing.JTextField();

        mniSelectAll.setText("Select all");
        mniSelectAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniSelectAllActionPerformed(evt);
            }
        });
        pmnAppDetails.add(mniSelectAll);

        mniDeselect.setText("Deselect");
        mniDeselect.setEnabled(false);
        mniDeselect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniDeselectActionPerformed(evt);
            }
        });
        pmnAppDetails.add(mniDeselect);
        pmnAppDetails.add(jSeparator1);

        mniFinish.setText("Finish");
        mniFinish.setEnabled(false);
        mniFinish.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniFinishActionPerformed(evt);
            }
        });
        pmnAppDetails.add(mniFinish);

        mniCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Update.png"))); // NOI18N
        mniCancel.setText("Cancel");
        mniCancel.setEnabled(false);
        mniCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniCancelActionPerformed(evt);
            }
        });
        pmnAppDetails.add(mniCancel);

        mniDelete.setText("Delete");
        mniDelete.setEnabled(false);
        mniDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mniDeleteActionPerformed(evt);
            }
        });
        pmnAppDetails.add(mniDelete);

        setClosable(true);
        setIconifiable(true);
        setTitle("Appointment management");
        setMaximumSize(new java.awt.Dimension(672, 634));
        setName("ManageApp"); // NOI18N

        pnlAppMaster.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.highlight"));
        pnlAppMaster.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "App master", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12), new java.awt.Color(51, 51, 51))); // NOI18N
        pnlAppMaster.setOpaque(false);

        lblCustName.setDisplayedMnemonic('r');
        lblCustName.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblCustName.setForeground(new java.awt.Color(0, 51, 255));
        lblCustName.setText("Customer name");
        lblCustName.setPreferredSize(new java.awt.Dimension(38, 20));

        chkHasApp.setForeground(new java.awt.Color(0, 102, 51));
        chkHasApp.setText("Already has an appointment");
        chkHasApp.setBorder(null);
        chkHasApp.setBorderPainted(true);
        chkHasApp.setEnabled(false);
        chkHasApp.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        chkHasApp.setOpaque(false);
        chkHasApp.setPreferredSize(new java.awt.Dimension(51, 20));

        btnMakeApp.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnMakeApp.setForeground(new java.awt.Color(0, 51, 51));
        btnMakeApp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/calendar.png"))); // NOI18N
        btnMakeApp.setMnemonic('x');
        btnMakeApp.setText("Make appointment");
        btnMakeApp.setToolTipText("Save to database");
        btnMakeApp.setBorder(null);
        btnMakeApp.setContentAreaFilled(false);
        btnMakeApp.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnMakeApp.setEnabled(false);
        btnMakeApp.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnMakeApp.setName(""); // NOI18N
        btnMakeApp.setOpaque(true);
        btnMakeApp.setPreferredSize(new java.awt.Dimension(130, 22));
        btnMakeApp.setRolloverEnabled(false);
        btnMakeApp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMakeAppActionPerformed(evt);
            }
        });
        pnlButton.add(btnMakeApp);

        btnAddSer.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnAddSer.setForeground(new java.awt.Color(0, 51, 51));
        btnAddSer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Insert.png"))); // NOI18N
        btnAddSer.setMnemonic('x');
        btnAddSer.setText("Add service");
        btnAddSer.setToolTipText("Save to database");
        btnAddSer.setBorder(null);
        btnAddSer.setContentAreaFilled(false);
        btnAddSer.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnAddSer.setEnabled(false);
        btnAddSer.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnAddSer.setName(""); // NOI18N
        btnAddSer.setPreferredSize(new java.awt.Dimension(100, 22));
        btnAddSer.setRolloverEnabled(false);
        btnAddSer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddSerActionPerformed(evt);
            }
        });
        pnlButton.add(btnAddSer);

        btnPreBill.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnPreBill.setForeground(new java.awt.Color(0, 51, 51));
        btnPreBill.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/bill.png"))); // NOI18N
        btnPreBill.setMnemonic('e');
        btnPreBill.setText("Pre-bill");
        btnPreBill.setToolTipText("Uncheck all select combo box");
        btnPreBill.setBorder(null);
        btnPreBill.setContentAreaFilled(false);
        btnPreBill.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnPreBill.setEnabled(false);
        btnPreBill.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnPreBill.setName(""); // NOI18N
        btnPreBill.setOpaque(true);
        btnPreBill.setPreferredSize(new java.awt.Dimension(59, 22));
        btnPreBill.setRolloverEnabled(false);
        btnPreBill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPreBillActionPerformed(evt);
            }
        });
        pnlButton.add(btnPreBill);

        btnFind.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnFind.setForeground(new java.awt.Color(0, 51, 51));
        btnFind.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Search.png"))); // NOI18N
        btnFind.setMnemonic('S');
        btnFind.setText("Find more ...");
        btnFind.setToolTipText("Find customer");
        btnFind.setBorder(null);
        btnFind.setContentAreaFilled(false);
        btnFind.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnFind.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnFind.setName(""); // NOI18N
        btnFind.setOpaque(true);
        btnFind.setPreferredSize(new java.awt.Dimension(100, 22));
        btnFind.setRolloverEnabled(false);
        btnFind.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFindActionPerformed(evt);
            }
        });
        pnlButton.add(btnFind);

        cboCust.setForeground(new java.awt.Color(0, 102, 51));
        cboCust.setModel(new javax.swing.DefaultComboBoxModel( new String[]{null}));
        cboCust.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboCustItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout pnlAppMasterLayout = new javax.swing.GroupLayout(pnlAppMaster);
        pnlAppMaster.setLayout(pnlAppMasterLayout);
        pnlAppMasterLayout.setHorizontalGroup(
            pnlAppMasterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlAppMasterLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlAppMasterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlAppMasterLayout.createSequentialGroup()
                        .addComponent(lblCustName, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cboCust, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(25, 25, 25)
                        .addComponent(chkHasApp, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32))
                    .addComponent(pnlButton, javax.swing.GroupLayout.PREFERRED_SIZE, 455, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(91, 91, 91))
        );
        pnlAppMasterLayout.setVerticalGroup(
            pnlAppMasterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAppMasterLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlAppMasterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chkHasApp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCustName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboCust, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46))
        );

        pnlAppDetails.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.highlight"));
        pnlAppDetails.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "App details", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12), new java.awt.Color(51, 51, 51))); // NOI18N
        pnlAppDetails.setOpaque(false);

        pnlDetailsChild.setBackground(new java.awt.Color(255, 255, 255));
        pnlDetailsChild.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        btnSelectAll.setBackground(new java.awt.Color(255, 255, 255));
        btnSelectAll.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnSelectAll.setForeground(new java.awt.Color(0, 51, 51));
        btnSelectAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/SelectAll.png"))); // NOI18N
        btnSelectAll.setMnemonic('a');
        btnSelectAll.setText("Select all");
        btnSelectAll.setToolTipText("Check all select combo box");
        btnSelectAll.setBorder(null);
        btnSelectAll.setContentAreaFilled(false);
        btnSelectAll.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnSelectAll.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnSelectAll.setName(""); // NOI18N
        btnSelectAll.setOpaque(true);
        btnSelectAll.setPreferredSize(new java.awt.Dimension(59, 22));
        btnSelectAll.setRolloverEnabled(false);
        btnSelectAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectAllActionPerformed(evt);
            }
        });

        btnDeselect.setBackground(new java.awt.Color(255, 255, 255));
        btnDeselect.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnDeselect.setForeground(new java.awt.Color(0, 51, 51));
        btnDeselect.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Deselect.png"))); // NOI18N
        btnDeselect.setMnemonic('e');
        btnDeselect.setText("Deselect");
        btnDeselect.setToolTipText("Uncheck all select combo box");
        btnDeselect.setBorder(null);
        btnDeselect.setContentAreaFilled(false);
        btnDeselect.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnDeselect.setEnabled(false);
        btnDeselect.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnDeselect.setName(""); // NOI18N
        btnDeselect.setOpaque(true);
        btnDeselect.setPreferredSize(new java.awt.Dimension(59, 22));
        btnDeselect.setRolloverEnabled(false);
        btnDeselect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeselectActionPerformed(evt);
            }
        });

        btnFinish.setBackground(new java.awt.Color(255, 255, 255));
        btnFinish.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnFinish.setForeground(new java.awt.Color(0, 51, 51));
        btnFinish.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Select.png"))); // NOI18N
        btnFinish.setMnemonic('S');
        btnFinish.setText("Finish");
        btnFinish.setToolTipText("Find customer");
        btnFinish.setBorder(null);
        btnFinish.setContentAreaFilled(false);
        btnFinish.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnFinish.setEnabled(false);
        btnFinish.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnFinish.setName(""); // NOI18N
        btnFinish.setOpaque(true);
        btnFinish.setPreferredSize(new java.awt.Dimension(100, 22));
        btnFinish.setRolloverEnabled(false);
        btnFinish.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFinishActionPerformed(evt);
            }
        });

        btnCancel.setBackground(new java.awt.Color(255, 255, 255));
        btnCancel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnCancel.setForeground(new java.awt.Color(0, 51, 51));
        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Cancel.png"))); // NOI18N
        btnCancel.setMnemonic('S');
        btnCancel.setText("Cancel");
        btnCancel.setToolTipText("Find customer");
        btnCancel.setBorder(null);
        btnCancel.setContentAreaFilled(false);
        btnCancel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnCancel.setEnabled(false);
        btnCancel.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnCancel.setName(""); // NOI18N
        btnCancel.setOpaque(true);
        btnCancel.setPreferredSize(new java.awt.Dimension(100, 22));
        btnCancel.setRolloverEnabled(false);
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnDelete.setBackground(new java.awt.Color(255, 255, 255));
        btnDelete.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnDelete.setForeground(new java.awt.Color(0, 51, 51));
        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Delete.png"))); // NOI18N
        btnDelete.setMnemonic('S');
        btnDelete.setText("Delete");
        btnDelete.setToolTipText("Find customer");
        btnDelete.setBorder(null);
        btnDelete.setContentAreaFilled(false);
        btnDelete.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnDelete.setEnabled(false);
        btnDelete.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnDelete.setName(""); // NOI18N
        btnDelete.setOpaque(true);
        btnDelete.setPreferredSize(new java.awt.Dimension(100, 22));
        btnDelete.setRolloverEnabled(false);
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        tblAppDetails.setForeground(new java.awt.Color(0, 102, 51));
        tblAppDetails.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "AppDID", "Select", "Slot", "Service", "Staff", "Status"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblAppDetails.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblAppDetails.getTableHeader().setReorderingAllowed(false);
        tblAppDetails.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblAppDetailsMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tblAppDetailsMousePressed(evt);
            }
        });
        jspAddDetails.setViewportView(tblAppDetails);
        if (tblAppDetails.getColumnModel().getColumnCount() > 0) {
            tblAppDetails.getColumnModel().getColumn(0).setMinWidth(0);
            tblAppDetails.getColumnModel().getColumn(0).setPreferredWidth(0);
            tblAppDetails.getColumnModel().getColumn(0).setMaxWidth(0);
            tblAppDetails.getColumnModel().getColumn(1).setPreferredWidth(30);
        }

        lblSerMsg.setForeground(new java.awt.Color(255, 255, 255));
        lblSerMsg.setText("Finished services won't be effect.");

        javax.swing.GroupLayout pnlDetailsChildLayout = new javax.swing.GroupLayout(pnlDetailsChild);
        pnlDetailsChild.setLayout(pnlDetailsChildLayout);
        pnlDetailsChildLayout.setHorizontalGroup(
            pnlDetailsChildLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDetailsChildLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlDetailsChildLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jspAddDetails, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(lblSerMsg, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(pnlDetailsChildLayout.createSequentialGroup()
                        .addComponent(btnSelectAll, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnDeselect, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnFinish, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        pnlDetailsChildLayout.setVerticalGroup(
            pnlDetailsChildLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlDetailsChildLayout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(pnlDetailsChildLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnFinish, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSelectAll, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDeselect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jspAddDetails, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblSerMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pnlService.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.highlight"));
        pnlService.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102)));
        pnlService.setOpaque(false);

        lblSlot.setDisplayedMnemonic('r');
        lblSlot.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblSlot.setForeground(new java.awt.Color(0, 51, 255));
        lblSlot.setText("Slot");
        lblSlot.setPreferredSize(new java.awt.Dimension(38, 20));

        lblCat.setDisplayedMnemonic('r');
        lblCat.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblCat.setForeground(new java.awt.Color(0, 51, 255));
        lblCat.setText("Category");
        lblCat.setPreferredSize(new java.awt.Dimension(38, 20));

        lblSer.setDisplayedMnemonic('r');
        lblSer.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblSer.setForeground(new java.awt.Color(0, 51, 255));
        lblSer.setText("Service");
        lblSer.setPreferredSize(new java.awt.Dimension(38, 20));

        lblStatus.setDisplayedMnemonic('r');
        lblStatus.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblStatus.setForeground(new java.awt.Color(0, 51, 255));
        lblStatus.setText("Status");
        lblStatus.setPreferredSize(new java.awt.Dimension(38, 20));

        cboSlot.setForeground(new java.awt.Color(0, 102, 51));
        cboSlot.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1: 8AM - 9AM", "Item 2", "Item 3", "Item 4" }));
        cboSlot.setEnabled(false);
        cboSlot.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboSlotItemStateChanged(evt);
            }
        });

        cboCat.setForeground(new java.awt.Color(0, 102, 51));
        cboCat.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Hair", "Item 2", "Item 3", "Item 4" }));
        cboCat.setEnabled(false);
        cboCat.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboCatItemStateChanged(evt);
            }
        });

        cboSer.setForeground(new java.awt.Color(0, 102, 51));
        cboSer.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Hair dye", "Item 2", "Item 3", "Item 4" }));
        cboSer.setEnabled(false);
        cboSer.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboSerItemStateChanged(evt);
            }
        });

        cboStatus.setForeground(new java.awt.Color(0, 102, 51));
        cboStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pending", "Item 2", "Item 3", "Item 4" }));
        cboStatus.setEnabled(false);

        lblStaff.setDisplayedMnemonic('r');
        lblStaff.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblStaff.setForeground(new java.awt.Color(0, 51, 255));
        lblStaff.setText("Staff to serve:");
        lblStaff.setPreferredSize(new java.awt.Dimension(38, 20));

        txtStaff.setEditable(false);
        txtStaff.setForeground(new java.awt.Color(0, 102, 51));
        txtStaff.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        txtStaff.setBorder(null);
        txtStaff.setPreferredSize(new java.awt.Dimension(56, 20));

        pnlStaffAvail.setBackground(new java.awt.Color(255, 255, 255));
        pnlStaffAvail.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        tblStaffAvail.setForeground(new java.awt.Color(0, 102, 51));
        tblStaffAvail.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "StaffID", "Staff", "Free slot"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblStaffAvail.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tblStaffAvail);
        if (tblStaffAvail.getColumnModel().getColumnCount() > 0) {
            tblStaffAvail.getColumnModel().getColumn(0).setMinWidth(0);
            tblStaffAvail.getColumnModel().getColumn(0).setPreferredWidth(0);
            tblStaffAvail.getColumnModel().getColumn(0).setMaxWidth(0);
            tblStaffAvail.getColumnModel().getColumn(1).setMinWidth(100);
            tblStaffAvail.getColumnModel().getColumn(1).setPreferredWidth(100);
            tblStaffAvail.getColumnModel().getColumn(1).setMaxWidth(100);
        }

        javax.swing.GroupLayout pnlStaffAvailLayout = new javax.swing.GroupLayout(pnlStaffAvail);
        pnlStaffAvail.setLayout(pnlStaffAvailLayout);
        pnlStaffAvailLayout.setHorizontalGroup(
            pnlStaffAvailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlStaffAvailLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        pnlStaffAvailLayout.setVerticalGroup(
            pnlStaffAvailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
        );

        lblRes.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblRes.setForeground(new java.awt.Color(0, 51, 255));
        lblRes.setText("Resources");

        btnSubmit.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnSubmit.setForeground(new java.awt.Color(0, 51, 51));
        btnSubmit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Submit.png"))); // NOI18N
        btnSubmit.setMnemonic('S');
        btnSubmit.setText("Submit");
        btnSubmit.setToolTipText("Find customer");
        btnSubmit.setBorder(null);
        btnSubmit.setContentAreaFilled(false);
        btnSubmit.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnSubmit.setEnabled(false);
        btnSubmit.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnSubmit.setName(""); // NOI18N
        btnSubmit.setOpaque(true);
        btnSubmit.setPreferredSize(new java.awt.Dimension(100, 22));
        btnSubmit.setRolloverEnabled(false);
        btnSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubmitActionPerformed(evt);
            }
        });

        btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/quit.png"))); // NOI18N
        btnExit.setMnemonic('x');
        btnExit.setText("Exit");
        btnExit.setToolTipText("Goodbye");
        btnExit.setBorder(null);
        btnExit.setContentAreaFilled(false);
        btnExit.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnExit.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnExit.setName(""); // NOI18N
        btnExit.setOpaque(true);
        btnExit.setPreferredSize(new java.awt.Dimension(59, 22));
        btnExit.setRolloverEnabled(false);
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        lblAvailFac.setForeground(new java.awt.Color(0, 102, 51));
        lblAvailFac.setText("Facs: 10");

        lblAvailStaff.setForeground(new java.awt.Color(0, 102, 51));
        lblAvailStaff.setText("Staff: 10");

        txtAppID.setEditable(false);
        txtAppID.setForeground(new java.awt.Color(240, 240, 240));
        txtAppID.setBorder(null);

        txtStatus.setEditable(false);
        txtStatus.setForeground(new java.awt.Color(240, 240, 240));
        txtStatus.setBorder(null);

        javax.swing.GroupLayout pnlServiceLayout = new javax.swing.GroupLayout(pnlService);
        pnlService.setLayout(pnlServiceLayout);
        pnlServiceLayout.setHorizontalGroup(
            pnlServiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlServiceLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlServiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblSer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblCat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblSlot, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblRes, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlServiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cboStatus, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cboSer, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cboSlot, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cboCat, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlServiceLayout.createSequentialGroup()
                        .addComponent(lblAvailFac)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblAvailStaff)))
                .addGap(18, 18, 18)
                .addGroup(pnlServiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlServiceLayout.createSequentialGroup()
                        .addComponent(txtAppID, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(36, 36, 36)
                        .addComponent(btnSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlServiceLayout.createSequentialGroup()
                        .addComponent(lblStaff, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtStaff, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(pnlServiceLayout.createSequentialGroup()
                        .addComponent(pnlStaffAvail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(7, 7, 7))
        );

        pnlServiceLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cboCat, cboSer, cboSlot, cboStatus});

        pnlServiceLayout.setVerticalGroup(
            pnlServiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlServiceLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlServiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlServiceLayout.createSequentialGroup()
                        .addGroup(pnlServiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblSlot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cboSlot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtStaff, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblStaff, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(lblCat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lblSer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lblStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlServiceLayout.createSequentialGroup()
                        .addComponent(cboCat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cboSer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cboStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(pnlStaffAvail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlServiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblRes, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblAvailFac)
                    .addComponent(lblAvailStaff)
                    .addComponent(btnSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtAppID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout pnlAppDetailsLayout = new javax.swing.GroupLayout(pnlAppDetails);
        pnlAppDetails.setLayout(pnlAppDetailsLayout);
        pnlAppDetailsLayout.setHorizontalGroup(
            pnlAppDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAppDetailsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlAppDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlDetailsChild, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(pnlAppDetailsLayout.createSequentialGroup()
                        .addComponent(pnlService, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        pnlAppDetailsLayout.setVerticalGroup(
            pnlAppDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAppDetailsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlService, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlDetailsChild, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(pnlAppDetails, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlAppMaster, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlAppMaster, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlAppDetails, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblAppDetailsMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAppDetailsMousePressed
        Point point = evt.getPoint();
        int currentRow = tblAppDetails.rowAtPoint(point);
        tblAppDetails.setRowSelectionInterval(currentRow, currentRow);
    }//GEN-LAST:event_tblAppDetailsMousePressed

    private void cboCustItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboCustItemStateChanged
        
        String custID = ((Customer)cboCust.getSelectedItem()).custID;
        
        if (custID != null) {
            boolean hasApp = hasApp(custID);
            beginState (hasApp);
            if (hasApp)
                bindingData(txtAppID.getText());
            else {
                serviceModel.setRowCount(0);
                lblSerMsg.setForeground(new Color(255,255,255));
            }
        } else {
            serviceModel.setRowCount(0);
            lblSerMsg.setForeground(new Color(255,255,255));
            beginState (false);
            btnMakeApp.setEnabled(false);
        }
        
        if (viewOnly) {
            btnMakeApp.setEnabled(false);
            btnAddSer.setEnabled(false);
            btnPreBill.setEnabled(false);
            btnSelectAll.setEnabled(false);
            mniSelectAll.setEnabled(false);
        }
    }//GEN-LAST:event_cboCustItemStateChanged
    
    private void bindingData(String appID) {
        int count = 0;
        serviceModel.setRowCount(0);
        String sql = "SELECT * FROM vw_full_app WHERE AppID = ?";
        param.clear();
        param.add(appID);
        
        dc.rs = dc.prepareSelect(dc.preSt_binApp, sql, param);
        
        try {
            while(dc.rs.next()){
                transpoter = new Vector();
                transpoter.add(dc.rs.getString("AppDID"));
                transpoter.add(false);
                transpoter.add(cboSlot.getItemAt(dc.rs.getInt("Slot")-1));
                transpoter.add(dc.rs.getString("ServiceName"));
                transpoter.add(dc.rs.getString("StaffName"));
                transpoter.add(dc.rs.getString("SttDetail"));
                serviceModel.addRow(transpoter);
                count++;
            }
        } catch (SQLException ex) {
            Logger.getLogger(FrmAppointment.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        if (count == 0)
            lblSerMsg.setText("No service found.");
        else
            lblSerMsg.setText(String.valueOf(count) + " services found.");
    }
    
    
    private void beginState (boolean hasApp) {
        if (hasApp) {
            btnMakeApp.setEnabled(false);
            btnAddSer.setEnabled(true);
            btnPreBill.setEnabled(true);
            
        } else {
            btnMakeApp.setEnabled(true);
            btnAddSer.setEnabled(false);
            btnPreBill.setEnabled(false);
        }
        chkHasApp.setSelected(hasApp);
        btnSelectAll.setEnabled(true);
        mniSelectAll.setEnabled(true);
        btnDeselect.setEnabled(false);
        mniDeselect.setEnabled(false);
        btnFinish.setEnabled(false);
        btnCancel.setEnabled(false);
        btnDelete.setEnabled(false);
        mniFinish.setEnabled(false);
        mniCancel.setEnabled(false);
        mniDelete.setEnabled(false);
    }
    
    private boolean hasApp(String custID) {
        String sql = "SELECT * FROM vw_cust_appmaster WHERE AppDate = ? AND CustID = ? AND Status IN (?,?)";
        param.clear();
        param.add(new java.sql.Date(new Date().getTime()));
        param.add(custID);
        param.add("Processing");
        param.add("Late-bill");
        
        dc.rs = dc.prepareSelect(dc.preSt_HasApp, sql, param);
        try {
            if (dc.rs.next()) {
                txtAppID.setText(dc.rs.getString("AppID"));
                txtStatus.setText(dc.rs.getString("Status"));
                lblSerMsg.setForeground(new Color(255,0,0));
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(FrmAppointment.class.getName()).log(Level.SEVERE, null, ex);
        }
        lblSerMsg.setForeground(new Color(255,255,255));
        return false;
    }
    
    private void newApp(String custID) {
        String sql = "INSERT INTO AppMaster(AppID, CustID, UserCreated) VALUES (?,?,?)";
        String appID = generateAppID();
        param.clear();
        param.add(appID);
        param.add(custID);
        param.add(User);
        
        dc.prepareUpdate(dc.preSt_newApp, sql, param);
        JOptionPane.showMessageDialog(this, "Appointment had been made.");
    }
    
    private String generateAppID() {
        String AppID, SufID;
        String MaskAppID = new SimpleDateFormat("yyyyMMdd").format(new Date())+"-App001";
        int IDNum;
        try {
            String sql = "SELECT TOP 1 AppID FROM AppMaster  WHERE AppID LIKE ? ORDER BY AppID DESC";
            param.clear();
            param.add("%"+MaskAppID.substring(0, 8)+"%");
            
            dc.rs = dc.prepareSelect(dc.preSt_genAppID, sql, param);
            
            if (dc.rs.next()) {
                AppID = dc.rs.getString("AppID");
                IDNum = Integer.parseInt(AppID.substring(12)); IDNum++;
                SufID = Integer.toString(IDNum);
                return MaskAppID.substring(0,15-SufID.length())+SufID;
            }
            return MaskAppID;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Connection error. Try again later.");
            return null;
        }
    }
    private void btnMakeAppActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMakeAppActionPerformed
        getAuthority();
        String custID = ((Customer)cboCust.getSelectedItem()).custID;
        newApp(custID);
        beginState(true);
    }//GEN-LAST:event_btnMakeAppActionPerformed
    
    private void enableForm(boolean enabled){
        cboSlot.setEnabled(enabled);
        cboCat.setEnabled(enabled);
        cboSer.setEnabled(enabled);
        cboStatus.setEnabled(enabled);
        btnSubmit.setEnabled(enabled);
    }
    private void btnAddSerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddSerActionPerformed
        enableForm(true);
        cboCatItemStateChanged(null);
        cboSerItemStateChanged(null);
    }//GEN-LAST:event_btnAddSerActionPerformed
    
    private void preBill(String appID, String status) {
        
        String sql, errMsg = "Appointment has not finished yet.";
        if (status.equalsIgnoreCase("Complete") || status.equalsIgnoreCase("Late-bill")) {
            sql = "UPDATE AppMaster SET Status = ?, DateModified = ?, UserModified = ? WHERE AppID = ?";
            param.clear();
            param.add("Pre-bill");
            param.add(new java.sql.Date(new Date().getTime()));
            param.add(User);
            param.add(appID);
            dc.prepareUpdate(dc.preSt_preBill, sql, param);
        } else {
            JOptionPane.showMessageDialog(this,errMsg);
        }
    }
    
    private void btnPreBillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPreBillActionPerformed
        String appID = txtAppID.getText();
        String status = txtStatus.getText();
        preBill(appID, status);
        btnPreBill.setEnabled(false);
    }//GEN-LAST:event_btnPreBillActionPerformed

    private void btnFindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFindActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnFindActionPerformed
    
    private int countRemainFac(String catName) {
        
        String sql = "SELECT COUNT(vw_cat_app.AppID) AS Served, Category.CatName, Category.Facilities AS Total "
                + "FROM Category LEFT OUTER JOIN vw_cat_app ON Category.CatID = vw_cat_app.CatID "
                + "WHERE AppID LIKE ? AND vw_cat_app.CatName = ? AND Status = 'Pending'"
                + "GROUP BY Category.Facilities, Category.CatName";
        
        param.clear();
        param.add("%"+new SimpleDateFormat("yyyyMMdd").format(new Date())+"%");
        param.add(catName);
        
        dc.rs = dc.prepareSelect(dc.preSt_CountFac, sql, param);
        
        try {
            
            if (dc.rs.next())
                return dc.rs.getInt("Total") - dc.rs.getInt("Served");
            else {
                sql = "SELECT Facilities FROM Category WHERE CatName = '"+catName + "'";
                dc.rs = dc.selectCommandNoPara(sql);
                dc.rs.next();
                return dc.rs.getInt("Facilities") ;
            }
        } catch (SQLException ex) {
            Logger.getLogger(FrmAppointment.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
    
    private int listStaff(String serName, int slot) {
        
        int count = 0;
        ResultSet rs;
        staffModel.setNumRows(0);
        txtStaff.setText("");
        Vector freeSlot;
        
        String staffID, staffName;
        String sql = "SELECT StaffID, StaffName FROM vw_staff_services WHERE Active = 'TRUE' AND IsLock = 'FALSE' "
                + "AND ServiceName = ? AND StaffID NOT IN "
                + "(SELECT StaffID FROM vw_full_app WHERE ServiceName = ? AND Slot = ? "
                + "AND SttDetail = 'Pending' AND AppID LIKE ?)";
        param.clear();
        param.add(serName);
        param.add(serName);
        param.add(slot);
        param.add("%"+new SimpleDateFormat("yyyyMMdd").format(new Date())+"%");
        
        dc.rs = dc.prepareSelect(dc.preSt_listStaff, sql, param);
        try {
            while (dc.rs.next()) {
                 staffID = dc.rs.getString("StaffID");
                 staffName = dc.rs.getString("StaffName");
                 count++;
                 String sql1 = "SELECT slot FROM vw_full_app WHERE StaffID = '" + staffID + "'"
                                                            + " AND ServiceName = '" + serName + "'"
                                                            + " AND SttDetail = 'Pending'"
                                                            + " AND AppID LIKE '%" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "%'";
                 rs = dc.selectCommandNoPara(sql1);
                 freeSlot = new Vector();
                 for (int i = 1; i <=8; i++)
                     freeSlot.add(i);
                 
                 rs.afterLast();
                 while (rs.previous())
                     freeSlot.remove(rs.getInt("slot")-1);
                     
                 showListStaff(staffID, staffName,freeSlot);
            }
        } catch (SQLException ex) {
            Logger.getLogger(FrmAppointment.class.getName()).log(Level.SEVERE, null, ex);
        }
        return count;
    }
    
    private void showListStaff(String staffID, String staffName, Vector freeSlot) {
        
        String freeSlotStr = "Slot ";
        for (int i = 0; i < freeSlot.size(); i++)
            freeSlotStr = freeSlotStr + freeSlot.get(i)+", ";
        
        transpoter = new Vector();
        transpoter.add(staffID);
        transpoter.add(staffName);
        transpoter.add(freeSlot);
        staffModel.addRow(transpoter);
    }
    private void cboCatItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboCatItemStateChanged
        String catName = (String)cboCat.getSelectedItem();
        remainFac = countRemainFac(catName);
        if (remainFac > 0)
            btnSubmit.setEnabled(true);
        else
            btnSubmit.setEnabled(false);
        lblAvailFac.setText("Facs: "+String.valueOf(remainFac));
        initSerComboBox();
    }//GEN-LAST:event_cboCatItemStateChanged

    private void cboSerItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboSerItemStateChanged
        String serName = ((Service)cboSer.getSelectedItem()).serName;
        int slot = cboSlot.getSelectedIndex()+1;
        availStaff = listStaff(serName, slot);
        if (availStaff > 0 && remainFac > 0 && cboCust.getSelectedIndex()!= 0)
            btnSubmit.setEnabled(true);
        else
            btnSubmit.setEnabled(false);
        lblAvailStaff.setText("Staff: "+String.valueOf(availStaff));
    }//GEN-LAST:event_cboSerItemStateChanged
    
    private void tblStaffValueChanged(ListSelectionEvent evt) {
        int rowSelected = tblStaffAvail.getSelectedRow();
        if (rowSelected != -1){
            txtStaff.setText((String)tblStaffAvail.getValueAt(tblStaffAvail.getSelectedRow(), 1));
        }
    }
    
    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnExitActionPerformed

    private void cboSlotItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboSlotItemStateChanged
        cboSerItemStateChanged(null);
    }//GEN-LAST:event_cboSlotItemStateChanged

    private void btnSelectAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectAllActionPerformed
        btnSelectAll.setEnabled(false);
        mniSelectAll.setEnabled(false);
        btnDeselect.setEnabled(true);
        mniDeselect.setEnabled(true);
        btnFinish.setEnabled(true);
        btnCancel.setEnabled(true);
        btnDelete.setEnabled(true);
        mniFinish.setEnabled(true);
        mniCancel.setEnabled(true);
        mniDelete.setEnabled(true);
        for (int i = 0; i < serviceModel.getRowCount(); i++)
            serviceModel.setValueAt(true, i, 1);
    }//GEN-LAST:event_btnSelectAllActionPerformed

    private void mniSelectAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniSelectAllActionPerformed
        btnSelectAllActionPerformed(null);
    }//GEN-LAST:event_mniSelectAllActionPerformed

    private void btnDeselectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeselectActionPerformed
        btnSelectAll.setEnabled(true);
        mniSelectAll.setEnabled(true);
        btnDeselect.setEnabled(false);
        mniDeselect.setEnabled(false);
        btnFinish.setEnabled(false);
        btnCancel.setEnabled(false);
        btnDelete.setEnabled(false);
        mniFinish.setEnabled(false);
        mniCancel.setEnabled(false);
        mniDelete.setEnabled(false);
        for (int i = 0; i < serviceModel.getRowCount(); i++)
            serviceModel.setValueAt(false, i, 1);
    }//GEN-LAST:event_btnDeselectActionPerformed

    private void mniDeselectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniDeselectActionPerformed
        btnDeselectActionPerformed(null);
    }//GEN-LAST:event_mniDeselectActionPerformed

    private void btnFinishActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFinishActionPerformed
        if ((JOptionPane.showConfirmDialog(this, "Are you sure want to change status to 'Finished' ?", "Service comfirm", JOptionPane.OK_CANCEL_OPTION))== JOptionPane.OK_OPTION){
            getAuthority();
            int count = 0;
            for (int row = 0; row < tblAppDetails.getRowCount(); row++){
                if (hasSelected(row)) {
                    finishSer(row);
                    count++;
                }
            }
        
            if (count == 0)
                JOptionPane.showMessageDialog(this, "Please select some records.");
        }
    }//GEN-LAST:event_btnFinishActionPerformed

    private void tblAppDetailsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAppDetailsMouseClicked
        int noRow = serviceModel.getRowCount();
        int noCount = 0;
        for (int i = 0; i < noRow; i++) {
            if ((boolean)serviceModel.getValueAt(i, 1) == true)
                noCount++;
        }
        
        if (noCount == noRow) {
            btnSelectAll.setEnabled(false);
            btnDeselect.setEnabled(true);
            mniSelectAll.setEnabled(false);
            mniDeselect.setEnabled(true);
            btnFinish.setEnabled(true);
            btnCancel.setEnabled(true);
            btnDelete.setEnabled(true);
            mniFinish.setEnabled(true);
            mniCancel.setEnabled(true);
            mniDelete.setEnabled(true);
        } else if (noCount == 0) {
            btnSelectAll.setEnabled(true);
            btnDeselect.setEnabled(false);
            mniSelectAll.setEnabled(true);
            mniDeselect.setEnabled(false);
            btnFinish.setEnabled(false);
            btnCancel.setEnabled(false);
            btnDelete.setEnabled(false);
            mniFinish.setEnabled(false);
            mniCancel.setEnabled(false);
            mniDelete.setEnabled(false);
        } else {
            btnSelectAll.setEnabled(true);
            btnDeselect.setEnabled(true);
            mniSelectAll.setEnabled(true);
            mniDeselect.setEnabled(true);
            btnFinish.setEnabled(true);
            btnCancel.setEnabled(true);
            btnDelete.setEnabled(true);
            mniFinish.setEnabled(true);
            mniCancel.setEnabled(true);
            mniDelete.setEnabled(true);
        }
    }//GEN-LAST:event_tblAppDetailsMouseClicked

    private void mniFinishActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniFinishActionPerformed
        btnFinishActionPerformed(null);
    }//GEN-LAST:event_mniFinishActionPerformed

    private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed
        if (!txtStaff.getText().isEmpty()) {
            saveData();
            bindingData(txtAppID.getText());
            txtStaff.setText("");
            staffModel.setNumRows(0);
        } else
            JOptionPane.showMessageDialog(this, "Please pick one staff to serve for current service.");
    }//GEN-LAST:event_btnSubmitActionPerformed

    private void saveData() {
        getAuthority();
        String slot, status, serName;
        
        for (int i = 0; i < tblAppDetails.getRowCount(); i++) {
            status = (String)tblAppDetails.getValueAt(i, 5);
            serName = (String)tblAppDetails.getValueAt(i, 3);
            slot = (String)tblAppDetails.getValueAt(i, 2);
            
            if (slot == (String)cboSlot.getSelectedItem() && status.equalsIgnoreCase("Pending")) {
                JOptionPane.showMessageDialog(this, "Slot "+cboSlot.getSelectedItem()+" already had a pending service.");
                return;
            }
            
            if (serName.equals(((Service)cboSer.getSelectedItem()).serName) && 
                    status.equalsIgnoreCase("Pending")) {
                JOptionPane.showMessageDialog(this, "'"+((Service)cboSer.getSelectedItem()).serName +
                                                    "' is already on service list.");
                return;
            }
        }
        
        String sql = "INSERT INTO AppDetails VALUES (?,?,?,?,?,?,?,?,?,?)";
        param.clear();
        param.add(generateAppDID(txtAppID.getText()));
        param.add(txtAppID.getText());
        param.add(((Service)cboSer.getSelectedItem()).serID);
        param.add(tblStaffAvail.getValueAt(tblStaffAvail.getSelectedRow(), 0));
        param.add(cboSlot.getSelectedIndex()+1);
        param.add(cboStatus.getSelectedItem());
        param.add(new java.sql.Date(new Date().getTime()));
        param.add(User);param.add(null);param.add(null);
        param.size();
        dc.prepareUpdate(dc.preSt_insertSer, sql, param);
        enableForm(false);
    }
    
    private String generateAppDID(String appID) {
        String AppDID, SufDID;
        String MaskAppDID = new SimpleDateFormat("yyyyMMdd").format(new Date())+"-App001.01";
        int IDNum;
        ArrayList vParam = new ArrayList();
        try {
            String sql = "SELECT TOP 1 AppDID FROM AppDetails  WHERE AppID = ? ORDER BY AppDID DESC";
            vParam.add(appID);
            
            dc.rs = dc.prepareSelect(dc.preSt_genAppDID, sql, vParam);
            
            if (dc.rs.next()) {
                AppDID = dc.rs.getString("AppDID");
                IDNum = Integer.parseInt(AppDID.substring(16)); IDNum++;
                SufDID = Integer.toString(IDNum);
                return MaskAppDID.substring(0,18-SufDID.length())+SufDID;
            }
            return MaskAppDID;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Connection error. Try again later.");
            return null;
        }
    }
    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        if ((JOptionPane.showConfirmDialog(this, "Are you sure want to change status to 'Canceled' ?", "Service comfirm", JOptionPane.OK_CANCEL_OPTION))== JOptionPane.OK_OPTION){
            getAuthority();
            int count = 0;
            for (int row = 0; row < tblAppDetails.getRowCount(); row++){
                if (hasSelected(row)) {
                    cancelSer(row);
                    count++;
                }
            }
        
            if (count == 0)
                JOptionPane.showMessageDialog(this, "Please select some records.");
        }
    }//GEN-LAST:event_btnCancelActionPerformed

    private void mniCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniCancelActionPerformed
        btnCancelActionPerformed(null);
    }//GEN-LAST:event_mniCancelActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        if ((JOptionPane.showConfirmDialog(this, "Are you sure want to delete selected Services?", "Service comfirm", JOptionPane.OK_CANCEL_OPTION))== JOptionPane.OK_OPTION){
            getAuthority();
            int count = 0;
            for (int row = (tblAppDetails.getRowCount()-1); row >= 0; row--){
                if (hasSelected(row)) {
                    deleteSer(row);
                    count++;
                }
            }
        
            if (count == 0)
                JOptionPane.showMessageDialog(this, "Please select some records.");
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void mniDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mniDeleteActionPerformed
        btnDeleteActionPerformed(null);
    }//GEN-LAST:event_mniDeleteActionPerformed
    
    private void deleteSer(int tableRow) {
        
        int row = tblAppDetails.convertRowIndexToModel(tableRow);
        String sql, status, AppDID;
        status = (String)serviceModel.getValueAt(row, 5);
        AppDID  = (String)serviceModel.getValueAt(row, 0);
        sql = "DELETE FROM AppDetails WHERE AppDID = ?";
        param.clear();
        param.add(AppDID);
        
        if (status.equalsIgnoreCase("Pending")) {
            
                dc.prepareUpdate(dc.preSt_delSer, sql, param);
                serviceModel.removeRow(row);
                lblSerMsg.setText("Delete successfully.");
        } else {
            serviceModel.setValueAt(false, row, 1);
            lblSerMsg.setText("Only pending service is executed.");
        }
            
    }
    
    private void finishSer(int tableRow) {
        int row = tblAppDetails.convertRowIndexToModel(tableRow);
        String sql, status, AppDID;
        status = (String)serviceModel.getValueAt(row, 5);
        AppDID = (String)serviceModel.getValueAt(row, 0);
        
        switch (status) {
            case "Pending":
                sql = "UPDATE AppDetails SET Status = ?, DateModified = ?, UserModified = ? WHERE AppDID = ?";
                param.clear();
                param.add("Finished");
                param.add(new java.sql.Date(new Date().getTime()));
                param.add(User);
                param.add(AppDID);
                dc.prepareUpdate(dc.preSt_updateSer, sql, param);
                serviceModel.setValueAt("Finished", row, 5);
                lblSerMsg.setText("Finish services successfully.");
                break;
            case "Finished":
            case "Canceled":
                lblSerMsg.setText("Only pending service is executed.");
                break;
        }
        serviceModel.setValueAt(false, row, 1);
    }
    
    private void cancelSer(int tableRow) {
        int row = tblAppDetails.convertRowIndexToModel(tableRow);
        String sql, status, AppDID;
        status = (String)serviceModel.getValueAt(row, 5);
        AppDID = (String)serviceModel.getValueAt(row, 0);
        
        switch (status) {
            case "Pending":
                sql = "UPDATE AppDetails SET Status = ?, DateModified = ?, UserModified = ? WHERE AppDID = ?";
                param.clear();
                param.add("Canceled");
                param.add(new java.sql.Date(new Date().getTime()));
                param.add(User);
                param.add(AppDID);
                dc.prepareUpdate(dc.preSt_updateSer, sql, param);
                serviceModel.setValueAt("Canceled", row, 5);
                lblSerMsg.setText("Cancel service successfully.");
                break;
            case "Finished":
            case "Canceled":
                lblSerMsg.setText("Only pending service is executed.");
                break;
        }
        serviceModel.setValueAt(false, row, 1);
    }
    
    private boolean hasSelected(int row) {
       row = tblAppDetails.convertRowIndexToModel(row);
       if ((boolean)serviceModel.getValueAt(row, 1) == true)
           return true;
       return false;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddSer;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnDeselect;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnFind;
    private javax.swing.JButton btnFinish;
    private javax.swing.JButton btnMakeApp;
    private javax.swing.JButton btnPreBill;
    private javax.swing.JButton btnSelectAll;
    private javax.swing.JButton btnSubmit;
    private javax.swing.JComboBox cboCat;
    private javax.swing.JComboBox cboCust;
    private javax.swing.JComboBox cboSer;
    private javax.swing.JComboBox cboSlot;
    private javax.swing.JComboBox cboStatus;
    private javax.swing.JCheckBox chkHasApp;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JScrollPane jspAddDetails;
    private javax.swing.JLabel lblAvailFac;
    private javax.swing.JLabel lblAvailStaff;
    private javax.swing.JLabel lblCat;
    private javax.swing.JLabel lblCustName;
    private javax.swing.JLabel lblRes;
    private javax.swing.JLabel lblSer;
    private javax.swing.JLabel lblSerMsg;
    private javax.swing.JLabel lblSlot;
    private javax.swing.JLabel lblStaff;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JMenuItem mniCancel;
    private javax.swing.JMenuItem mniDelete;
    private javax.swing.JMenuItem mniDeselect;
    private javax.swing.JMenuItem mniFinish;
    private javax.swing.JMenuItem mniSelectAll;
    private javax.swing.JPopupMenu pmnAppDetails;
    private javax.swing.JPanel pnlAppDetails;
    private javax.swing.JPanel pnlAppMaster;
    private javax.swing.JPanel pnlButton;
    private javax.swing.JPanel pnlDetailsChild;
    private javax.swing.JPanel pnlService;
    private javax.swing.JPanel pnlStaffAvail;
    private javax.swing.JTable tblAppDetails;
    private javax.swing.JTable tblStaffAvail;
    private javax.swing.JTextField txtAppID;
    private javax.swing.JTextField txtStaff;
    private javax.swing.JTextField txtStatus;
    // End of variables declaration//GEN-END:variables
}

class Customer {
    String custID;
    String custName;
    
    public Customer (String ci, String cn) {
        this.custID = ci;
        this.custName = cn;
    }
}

class Service {
    String serID;
    String serName;
    
    public Service (String ci, String cn) {
        this.serID = ci;
        this.serName = cn;
    }
}

class cboCustRender extends BasicComboBoxRenderer {
    @Override
    public Component getListCellRendererComponent(  
            JList list, Object value, int index,  
            boolean isSelected, boolean cellHasFocus)  
        {  
            super.getListCellRendererComponent(list, value, index,  
                isSelected, cellHasFocus);  
  
            if (value != null)  
            {  
                Customer cust = (Customer)value;  
                setText(cust.custName);
                
            }
            if (isSelected)
                setBackground(new Color(0,102,51));
            
            return this;  
        }  
}

class cboSerRender extends BasicComboBoxRenderer {
    @Override
    public Component getListCellRendererComponent(  
            JList list, Object value, int index,  
            boolean isSelected, boolean cellHasFocus)  
        {  
            super.getListCellRendererComponent(list, value, index,  
                isSelected, cellHasFocus);  
  
            if (value != null)  
            {  
                Service ser = (Service)value;  
                setText(ser.serName);
                
            }
            if (isSelected)
                setBackground(new Color(0,102,51));
            
            return this;  
        }  
}

class cboRender extends BasicComboBoxRenderer {
    @Override
    public Component getListCellRendererComponent(  
            JList list, Object value, int index,  
            boolean isSelected, boolean cellHasFocus)  
        {  
            super.getListCellRendererComponent(list, value, index,  
                isSelected, cellHasFocus);  
  
            if (value != null)  
            {  
                String item = (String)value;  
                setText(item);
                
            }
            if (isSelected)
                setBackground(new Color(0,102,51));
            
            return this;  
        }  
}

class NullSelectionModel implements ListSelectionModel {  
    public boolean isSelectionEmpty() { return true; }  
    public boolean isSelectedIndex(int index) { return false; }  
    public int getMinSelectionIndex() { return -1; }  
    public int getMaxSelectionIndex() { return -1; }  
    public int getLeadSelectionIndex() { return -1; }  
    public int getAnchorSelectionIndex() { return -1; }  
    public void setSelectionInterval(int index0, int index1) { }  
    public void setLeadSelectionIndex(int index) { }  
    public void setAnchorSelectionIndex(int index) { }  
    public void addSelectionInterval(int index0, int index1) { }  
    public void insertIndexInterval(int index, int length, boolean before) { }  
    public void clearSelection() { }  
    public void removeSelectionInterval(int index0, int index1) { }  
    public void removeIndexInterval(int index0, int index1) { }  
    public void setSelectionMode(int selectionMode) { }  
    public int getSelectionMode() { return SINGLE_SELECTION; }  
    public void addListSelectionListener(ListSelectionListener lsl) { }  
    public void removeListSelectionListener(ListSelectionListener lsl) { }  
    public void setValueIsAdjusting(boolean valueIsAdjusting) { }  
    public boolean getValueIsAdjusting() { return false; }  
}